<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" 
        :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增供应商</el-button>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
          >
            <el-table-column prop="id" label="ID" width="60"/>
            <el-table-column prop="name" label="供应商名称" min-width="150"/>
            <el-table-column prop="username" label="登录用户名" width="120"/>
            <el-table-column prop="contact_person" label="联系人" width="100"/>
            <el-table-column prop="phone" label="联系电话" width="120"/>
            <el-table-column prop="address" label="地址" min-width="200" show-overflow-tooltip/>
            <!-- <el-table-column prop="status" label="状态" width="100">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.status === 1" type="success">正常</el-tag>
                <el-tag v-if="scope.row.status === 0" type="danger">禁用</el-tag>
              </template>
            </el-table-column> -->
            <el-table-column prop="create_time" label="创建时间" width="160">
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="150">
              <template slot-scope="scope">
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" @click="onPassword(scope.row)">重置密码</el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
    <Add ref="addFormRef" @get-page="initData" />
  </div>
</template>

<script>
import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";

export default {
  name: "SupplierIndex",
  components: {
    Add
  },
  mixins: [crud],
  data() {
    return {
      request: http.supplier,
      searchList: [
        {
          label: '供应商名称',
          prop: 'name',
          name: 'input',
          placeholder: '请输入供应商名称'
        },
        {
          label: '联系人',
          prop: 'contact_person',
          name: 'input',
          placeholder: '请输入联系人'
        },
        {
          label: '联系电话',
          prop: 'phone',
          name: 'input',
          placeholder: '请输入联系电话'
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          options: [
            { value: 1, label: '正常' },
            { value: 0, label: '禁用' }
          ]
        }
      ],
      searchParams: {
        name: '',
        contact_person: '',
        phone: '',
        status: ''
      },
      defaultParams: {
      },
      tableData: []
    }
  },
  methods: {
    onPassword(row) {
      this.$prompt('请输入新密码', '重置密码', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputType: 'password',
        inputPattern: /.+/,
        inputErrorMessage: '密码不能为空'
      }).then(({ value }) => {
        this.resetPassword(row.id, value);
      }).catch(() => {
        // 用户取消
      });
    },

    async resetPassword(supplierId, newPassword) {
      try {
        await http.supplier.update(supplierId, {
          password: newPassword
        });
        this.$message.success('密码重置成功');
      } catch (error) {
        console.error('重置密码失败:', error);
        this.$message.error('重置密码失败: ' + (error.message || '未知错误'));
      }
    }
  }
}
</script>

<style scoped lang="scss">
.table-layout {
  margin-top: 20px;
}
.table-handle {
  margin-bottom: 20px;
}
.page-box {
  margin-top: 20px;
  text-align: right;
}
</style> 